angular.module('inspinia', ['angularFileUpload']).controller('wechatPublicCtrl', function ($scope, $http, SweetAlert, $state, $stateParams, i18nService, $timeout, FileUploader) {
  //数据源
  i18nService.setCurrentLang('zh-cn');


  $scope.titles = [{value: "", text: "请选择"}];
  $scope.pubNames = [{value: "", text: "请选择"}];

  // 重置按钮
  $scope.resetForm = function () {
    $scope.info = {title: "", pubAcc: ""};
  };
  $scope.resetForm();


  // 查询分页
  $scope.paginationOptions = angular.copy($scope.paginationOptions);
  $scope.wetchatPublicGrid = {
    paginationPageSize: 10,                  //分页数量
    paginationPageSizes: [10, 20, 50, 100],	  //切换每页记录数
    useExternalPagination: true,		  //开启拓展名
    enableHorizontalScrollbar: true,        //横向滚动条
    enableVerticalScrollbar: true,  		//纵向滚动条
    columnDefs: [
      {field: 'id', displayName: '序号', pinnable: false, width: 120, sortable: false},
      {field: 'title', displayName: '模板标题', pinnable: false, width: 280, sortable: false},
      {field: 'msgTempId', displayName: '模板id', pinnable: false, width: 380, sortable: false},
      {field: 'pubName', displayName: '公众号', pinnable: false, width: 280, sortable: false},
      {
        field: 'createTime',
        displayName: '创建时间',
        pinnable: false,
        width: 150,
        sortable: false,
        cellFilter: 'date:"yyyy-MM-dd HH:mm:ss"'
      },

      {
        field: 'action',
        displayName: '操作',
        width: 250,
        pinnedRight: true,
        sortable: false,
        editable: true,
        cellTemplate:
          '<div class="lh30">'
          + '<a class="lh30" ng-show="grid.appScope.hasPermit(\'publicAccountMsgTemp.pubSend\')" ui-sref="sys.wechatPublicDetail({id:row.entity.id})" >发送信息</a>'
          + '</div>'
      }
    ],
    onRegisterApi: function (gridApi) {
      $scope.gridApi = gridApi;
      $scope.gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
        $scope.paginationOptions.pageNo = newPage;
        $scope.paginationOptions.pageSize = pageSize;
        $scope.query();
      });
    }
  };

  // 查询按钮
  $scope.query = function () {
    if ($scope.loadImg) {
      return;
    }
    $scope.loadImg = true;
    $http.post('publicAccountMsgTemp/list',
      "info=" + angular.toJson($scope.info) + "&pageNo=" + $scope.paginationOptions.pageNo + "&pageSize=" +
      $scope.paginationOptions.pageSize, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
      .success(function (result) {
        if (result.status) {
          $scope.wetchatPublicGrid.data = result.data.result;
          $scope.wetchatPublicGrid.totalItems = result.data.totalCount;
        } else {
          $scope.notice(result.msg);
        }
        $scope.loadImg = false;
      }).error(function () {
      $scope.notice("系统异常！");
    });
  };

  $scope.query();


  /***
   * 获取列表查询条件
   */
  $scope.queryCondition = function () {
    $http.post('publicAccountMsgTemp/queryCondition',
      null, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
      .success(function (result) {
        if (result.status) {
          if (result.data.titles && result.data.titles.length > 0) {
            result.data.titles.forEach(data => {
              $scope.titles.push({value: data.title, text: data.title});
            })

          }
          if (result.data.pubNames && result.data.pubNames.length > 0) {
            result.data.pubNames.forEach(data => {
              $scope.pubNames.push({value: data.pubAcc, text: data.pubName});
            })
          }
        } else {
          $scope.notice(result.msg);
        }
      }).error(function () {
      $scope.notice("系统异常！");
    });
  };

  $scope.queryCondition();


  /**
   * 提交公众号模板消息推送批次记录表
   */
  $scope.submitBatchInfo = function () {
    $scope.hideSendModal();
    $scope.submitting = true;
    $scope.batchInfo.tempId = $scope.detailInfo.id;
    $http({
      url: 'publicAccountMsgBatch/save',
      method: 'POST',
      data: {info: $scope.batchInfo}
    }).success(function (result) {
      if (result.status) {
        $scope.submitting = false;
        $state.transitionTo('workOrder.workOrderManager', null, {reload: false});
        $scope.notice(result.msg);
      } else {
        $scope.submitting = false;
        $scope.notice(result.msg);
      }
    });
  }


});


